<template>
  <div class="login">
    <div class="main">
      <div class="left">
        <img
          src="@/assets/login01.png"
        />
      </div>
      <div class="center">
        <div class="text">
          <p>登录</p>
          <p>卷上天医疗服务后台管理系统</p>
        </div>
        <el-form
          :model="ruleForm"
          status-icon
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          hide-required-asterisk
        >
          <el-form-item label="账号" prop="username">
            <el-input
              type="text"
              placeholder="请输入您的账号"
              v-model="ruleForm.username"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input
              type="password"
              placeholder="请输入您的密码"
              v-model="ruleForm.password"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <span>忘记密码请联系管理员</span>
          <button type="primary" @click.prevent="onsubmitForm('ruleForm')">
            提交
          </button>
        </el-form>
      </div>
      <div class="right">
        <img
          src="@/assets/login02.png"
        />
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          { required: true, message: "账号不能为空" },
          { validator: this.validateUsername },
        ],
        password: [
          { required: true, message: "密码不能为空" },
          { validator: this.validatePassword },
        ],
      },
    };
  },
  methods: {
    validateUsername(rule, value, callback) {
      if (/^\s+$/.test(value)) {
        callback(new Error("账号中不能包含空格"));
      } else {
        callback();
      }
    },
    validatePassword(rule, value, callback) {
      if (/^\s+$/.test(value)) {
        callback(new Error("密码中不能包含空格"));
      } else {
        callback();
      }
    },
    onsubmitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        // console.log(valid);
        if (valid) {
          // console.log(this.ruleForm);
          let bloo = await this.$store.dispatch("user/loginFn", this.ruleForm);
          // console.log("login.bloo", bloo);
          if (bloo) {
            this.$router.push("/");
          } else {
            alert("账户和密码需要均为 admin 或 user !!!");
          }
        } else {
          alert("error!!");
          return false;
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
@import "./scss/index.scss";
</style>